<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>
Giỏ hàng
</title>
<LINK href="style.css" rel=stylesheet>
<META http-equiv=Content-Type
content="text/html; charset=UTF-8">
<style type="text/css">
body,td,th {
	font-family: "Times New Roman", Times, serif;
}
a {
	font-family: "Times New Roman", Times, serif;
}
</style>
</head>
<body bottomMargin=0 leftMargin=0
topMargin=0 rightMargin=0>
	<TABLE width="1024" height="768" border=0 cellSpacing=0 cellPadding=0 align=center>
	<TR HEIGHT="150">
	<TD Align=center colspan=5>
	<%@include file="top.jsp"%>
	</TD>
	</TR>
	
	<!---Khai báo khoảng phân cách--->
	<TR HEIGHT="2">
	<TD colspan=5 bgcolor="white"></TD>
	</TR>
	
	<TR HEIGHT=100%>
	<TD vAlign="top" width="150">
	<%@include file="left.jsp"%>
	</TD>
	
	<!---Khai báo khoảng phân cách--->
	<TD bgcolor=white width="2"></TD>
	
	<TD vAlign=top width="720" bgcolor=white>
	<p align=center><font color=blue size=5>Giỏ hàng của bạn</font></p>

<%!
	boolean notExist(int a[], int b) {
		for (int i = 0; i < a.length; i++) {
			if (a[i] == b) return false;
		}
		return true;
	}
%>

<%
	Class.forName("com.mysql.jdbc.Driver");
	Connection con = null;
	Statement smt = null;
	ResultSet rst = null;
	int i = 0;
	int total = 0;
	String cart[] = (String[])session.getValue("cart");
	Boolean exist = (Boolean)session.getValue("exist");
	String del = request.getParameter("del");
	if (del != null && del.equals("1")) {
		out.println("<p align=center><font size=5 color=red>Giỏ hàng đã bị xóa!</font></p>");
	}
	if (exist != null && exist) {
		out.println("<p align=center><font size=5 color=red>Đã có trong giỏ hàng!</font><p>");
		session.putValue("exist", false);
	}
	try {
		String username = "root";
		String password = "Vietyeutam1112";
		String url = "jdbc:mysql://127.0.0.1:3306/banhang";
		con = DriverManager.getConnection(url, username, password);
		System.out.println("Da ket noi CSDL");
		smt = con.createStatement();
		out.println("<table width=100% border=1>");
		out.println("<tr width=100% border=1>");
		out.println("<td height=10 align=center>Mã Sản Phẩm</td>");
		out.println("<td height=10 align=center>Tên Sản Phẩm</td>");
		out.println("<td height=10 align=center>Hãng Sản Xuất</td>");
		out.println("<td height=10 align=center>Giá</td>");
		out.println("</tr>");
		if (cart != null)
		while (cart[i]!=null) {
			rst = smt.executeQuery("select * from product where ID='"+cart[i]+"'");
			rst.next();
			out.println("<tr width=100% >");
			out.println("<td align=center>"+rst.getString(1)+"</td>");
			out.println("<td align=center>"+rst.getString(2)+"</td>");
			out.println("<td align=center>"+rst.getString(3)+"</td>");
			out.println("<td align=center>"+rst.getString(4)+"</td>");
			out.println("</tr>");
			total += Integer.parseInt(rst.getString(4));
			i++;
		}
		else {
			out.println("<tr><td colspan=4 align=center>");
			out.println("<font color=red>Giỏ hàng trống!</font>");
			out.println("</td></tr>");
		}
		out.println("<tr><td colspan=4 align=center>");
		out.println("<font color=red>Thành tiền: "+total+"</font>");
		out.println("</td></tr>");
		out.println("</table>");
		con.close();
		System.out.println("Dong ket noi");
	}
	catch (Exception e) {
		System.out.println("KHONG KET NOI DUOC");
	}
%>
	<table width=100%>
		<tr height="50">
			<td align=right width=50% valign="bottom">
			<form action="deletecart.jsp" method="GET"><input type="submit" value="Hủy giỏ hàng"></form>
			</td>
			<td align=left width=50% valign="bottom">
			<form action="checkout.jsp" method="GET"><input type="submit" value="Đặt hàng"></form>
			</td>
		</tr>
	</table>
	</TD>
	
	<!---Khai báo khoảng phân cách--->
	<TD bgcolor=white width="2"></TD>
	<TD vAlign=top width="150">
	<%@include file="right.jsp"%>
	</TD>
	</TR>
	
	<!---Khai báo khoảng phân cách--->
	<TR HEIGHT="2">
	<TD colspan=5 bgcolor=white></TD>
	</TR>
	
	<TR HEIGHT="50">
	<TD colspan=5 align=center>
	<%@include file="bottom.jsp"%>
	</TD>
	</TR>
	</TABLE>
</body>
</html>